package util;


import javax.servlet.*;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import orm.User;

public class OnLine implements HttpSessionBindingListener{
    private User user;
    private OnlineUsers onlineusers = OnlineUsers.getInstance();
    public void setUser(User user){
    	this.user=user;
    }
    public User getUser()
    {
    	return user;
    }
	public void valueBound(HttpSessionBindingEvent se) {
		HttpSession session=se.getSession();
		ServletContext sct=session.getServletContext();
		onlineusers = (OnlineUsers)sct.getAttribute("onlinetesters");
		if(onlineusers==null)
		{
			onlineusers = OnlineUsers.getInstance();
			onlineusers.addUser(user);
		}
		else{
			if(!onlineusers.getOnlineUsers().contains(user))
			{
				onlineusers.addUser(user);
			}
		}
		sct.setAttribute("onlinetesters", onlineusers);
	}
	public void valueUnbound(HttpSessionBindingEvent se) {
		HttpSession session=se.getSession();
		ServletContext sct=session.getServletContext();
        onlineusers = (OnlineUsers)sct.getAttribute("onlinetesters");
		if(user!=null)
			onlineusers.removeUser(user); 
		sct.setAttribute("onlinetesters", onlineusers);
			
	}

}
